package com.tanxi521.springboot.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class FileUploadUtils {

    public static String uploadHeadImg(MultipartFile multipartFile) {
        String filename = multipartFile.getOriginalFilename();
//        String path = "D:\\MC服务器\\web\\user\\user_head";
        String path = null;
        String filePath = "D:/MC服务器/web/tn_admin/";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String datePath = sdf.format(new Date());
        String imagePath = "images/" + datePath;
        String otherPath = "other/" + datePath;
        String viewPath = "http://mc.tanxi521.com:9007/image/";
        boolean isImg = filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".png");
        if (isImg) {
            path = filePath + imagePath;
        } else {
            path = filePath + otherPath;
        }
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdir = file.mkdirs();
        }
        String timeStamp = String.valueOf(System.currentTimeMillis());
        filename = timeStamp + filename.substring(filename.lastIndexOf("."), filename.length());
        try {
            multipartFile.transferTo(new File(path + File.separator + filename));
            if (isImg) {
                return viewPath + imagePath + File.separator + filename;
            } else {
                return viewPath + otherPath + File.separator + filename;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "500";
        }
    }
}
